﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.IO;
using System.Xml;
using WindowsFormsApplication1.RebornLogin;

namespace WindowsFormsApplication1
{
    public class WorldsChangeRequest
    {
        private List<WorldChangeRequest> results = new List<WorldChangeRequest>();
        //private Timer requestTimeout = new Timer(5000);

        //public bool pendingRequest = false;

        public WorldsChangeRequest()
        {
            //requestTimeout.AutoReset = false;
            //requestTimeout.Elapsed += new ElapsedEventHandler(requestTimeout_Elapsed);
        }

        //private void requestTimeout_Elapsed(object sender, ElapsedEventArgs e)
        //{
            
        //}

        public void addRequest(ServerSocketData ssd, List<OwnedWorld> ownedWorlds)
        {
            WorldChangeRequest result = new WorldChangeRequest();
            result.serverData = ssd;
            result.ownedWorlds = ownedWorlds;

            results.Add(result);
        }
        public void sendRequests()
        {
            foreach (WorldChangeRequest result in results)
            {
                string xmlMessage = "";

                StringWriter stringWriter = new StringWriter();
                XmlTextWriter xmldoc = new XmlTextWriter(stringWriter);

                xmldoc.WriteStartElement("ownedworlds");
                foreach (OwnedWorld world in result.ownedWorlds)
                    xmldoc.WriteRaw(world.toXML());
                xmldoc.WriteEndElement();

                xmldoc.Flush();

                xmldoc.Close();

                stringWriter.Flush();

                xmlMessage = stringWriter.ToString();

                SocketConnectionsManager.sendToServer(result.serverData, xmlMessage);
            }
            //requestTimeout.Start();
        }
    }
    public class WorldChangeRequest
    {
        public List<OwnedWorld> ownedWorlds;
        public ServerSocketData serverData;
        //public bool hasResponded = false;
    }
}
